package com.nanshenycd.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nanshenycd.pojo.Tag;
import com.nanshenycd.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

@Controller
@Api(description  = "后台标签管理接口")
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;

    @GetMapping("/tags")
    @ApiOperation(value = "展示标签页面")
    //通过PageableDefault指定每页数据，排序方式,正序倒序
    public String list(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Tag> tags = tagService.listTag();

        //得到分页结果对象
        PageInfo<Tag> pageInfo = new PageInfo<>(tags);

        //将查询出来的每页的数据封装到model中
        model.addAttribute("pageInfo", pageInfo);
        return "admin/tags";
    }

    //跳转新增页面
    @GetMapping("/tags/input")
    @ApiOperation(value = "跳转添加标签页面")
    public String toAdd(Model model) {
        model.addAttribute("tag", new Tag());
        return "admin/tags-input";
    }

    @PostMapping("/tags/add")
    //接口复用
    @ApiOperation(value = "添加标签检验接口")
    public String add(@Valid Tag tag, BindingResult result, RedirectAttributes attributes) {
        //重复校验
        Tag tag2 = tagService.getTagByName(tag.getName());
        if (tag2 != null) {
            attributes.addFlashAttribute("message", "该分类已经存在");
            return "redirect:/admin/tags/input";
        }
        //@Valid 代表需要校验是否为空 ,BindingResult 代表检验后的结果
        if (result.hasErrors()) {
            return "admin/tags-input";
        }
        int i = tagService.saveTag(tag);
        if (i < 0) { //如果没保存进去给出提示
            attributes.addFlashAttribute("message", "添加失败");
        } else {
            attributes.addFlashAttribute("message", "添加成功");
        }
        return "redirect:/admin/tags";
    }


    //修改
    @GetMapping("/tags/{id}/input")
    @ApiOperation(value = "编辑对应的标签页面")
    public String editInput(@PathVariable Long id, Model model) {
        Tag tag = tagService.getTagById(id);
        model.addAttribute("tag", tag);
        return "admin/tags-update";
    }

    //修改操作
    @PostMapping("/tags/update")
    @ApiOperation(value = "修改内容提交操作")
    public String editPost(@Valid Tag tag, BindingResult result, RedirectAttributes attributes,Model model) {
        //重复校验
        Tag tag2 = tagService.getTagByName(tag.getName());
        if (tag2 != null) {
//            attributes.addFlashAttribute("message", "该分类已经存在");
            model.addAttribute("message","请勿输入已有分类");
            return "/admin/tags-update";
        }

        int i = tagService.updateTag(tag);
        if (i < 0) { //如果没保存进去给出提示
            attributes.addFlashAttribute("message", "更新失败");
        } else {
            attributes.addFlashAttribute("message", "更新成功");
        }
        return "redirect:/admin/tags";
    }


    @GetMapping("/tags/{id}/delete")
    @ApiOperation(value = "删除对应的标签")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        tagService.deleteTag(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/tags";
    }
}
